package edu.ccut.titan.tag.chart.utils;

import java.util.List;
import java.util.GregorianCalendar;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

/**
 * Created by IntelliJ IDEA.
 * User: Leslie
 * Date: 2005-11-24
 * Time: 15:55:07
 * Descriptions: 用于处理与时间粒度有关的操作
 */
public class TimeGranularity {

    public static final int DATE = 0;
    public static final int TIME = 1;
    public static final int DATE_TIME = 3;

    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public static final int DEFAULT_GRANULARITY = 5;

    public static final String DEFAULT_START_TIME = "00:00:00";
    public static final String DEFAULT_END_TIME = "23:59:59";

    private String startTimeString;
    private String endTimeString;
    private String dateFormat;
    private String timeFormat;
    private int granularity;
    private int returnType;

    public TimeGranularity(String startTimeString, String endTimeString, String dateFormat,
                           String timeFormat, int granularity, int returnType) {
        this.startTimeString = startTimeString;
        this.endTimeString = endTimeString;
        this.dateFormat = dateFormat;
        this.timeFormat = timeFormat;
        this.granularity = granularity;
        this.returnType = returnType;
    }

    public TimeGranularity(String startTimeString, String endTimeString) {
        this(startTimeString, endTimeString, DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT, DEFAULT_GRANULARITY, DATE_TIME);
    }

    public TimeGranularity(String dateFormat, String timeFormat, int granularity) {
        this(DEFAULT_START_TIME, DEFAULT_END_TIME, dateFormat, timeFormat, granularity, DATE_TIME);
    }

    public TimeGranularity() {
        this(DEFAULT_DATE_FORMAT, DEFAULT_TIME_FORMAT, DEFAULT_GRANULARITY);
    }

    public String getStartTimeString() {
        return startTimeString;
    }

    public void setStartTimeString(String startTimeString) {
        this.startTimeString = startTimeString;
    }

    public String getEndTimeString() {
        return endTimeString;
    }

    public void setEndTimeString(String endTimeString) {
        this.endTimeString = endTimeString;
    }

    public String getDateFormat() {
        return dateFormat;
    }

    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }

    public String getTimeFormat() {
        return timeFormat;
    }

    public void setTimeFormat(String timeFormat) {
        this.timeFormat = timeFormat;
    }

    public int getGranularity() {
        return granularity;
    }

    public void setGranularity(int granularity) {
        this.granularity = granularity;
    }

    public int getReturnType() {
        return returnType;
    }

    public void setReturnType(int returnType) {
        this.returnType = returnType;
    }

    public List<String> getGranularityList() {

        if (granularity > 3600 || granularity <= 0) {
            return null;
        }

        GregorianCalendar startTime = new GregorianCalendar();
        GregorianCalendar endTime = new GregorianCalendar();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        SimpleDateFormat simpleTimeFormat = new SimpleDateFormat(timeFormat);
        SimpleDateFormat simpleAllFormat = new SimpleDateFormat(dateFormat + " " + timeFormat);
        GregorianCalendar tmpCalendar = new GregorianCalendar();
        String date = simpleDateFormat.format(tmpCalendar.getTime()).trim();

        try {
            startTime.setTime(simpleAllFormat.parse(date + " " + startTimeString));
            endTime.setTime(simpleAllFormat.parse(date + " " + endTimeString));
            endTime.add(GregorianCalendar.MINUTE, 1);

            List<String> granularityList = new ArrayList<String>();
            for (tmpCalendar.setTime(startTime.getTime()); tmpCalendar.before(endTime);) {
                switch (returnType) {
                    case DATE:
                        granularityList.add(simpleDateFormat.format(tmpCalendar.getTime()).trim());
                        break;
                    case TIME:
                        granularityList.add(simpleTimeFormat.format(tmpCalendar.getTime()).trim());
                        break;
                    case DATE_TIME:
                        granularityList.add(simpleAllFormat.format(tmpCalendar.getTime()).trim());
                        break;
                }
                tmpCalendar.add(GregorianCalendar.MINUTE, granularity);
            }
            return granularityList;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将oriDate的日期与desDate的时间联合生成新的时间
     * @param desDate
     * @param oriDate
     * @return
     */
    public static Date ConvertToSameDate(Date oriDate, Date desDate) {
        try {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TimeGranularity.DEFAULT_DATE_FORMAT);
        SimpleDateFormat simpleTimeFormat = new SimpleDateFormat(TimeGranularity.DEFAULT_TIME_FORMAT);
        SimpleDateFormat simpleAllFormat = new SimpleDateFormat(TimeGranularity.DEFAULT_DATE_FORMAT
                + " " + TimeGranularity.DEFAULT_TIME_FORMAT);

        String dateString = simpleDateFormat.format(oriDate);
        String timeString = simpleTimeFormat.format(desDate);

            return simpleAllFormat.parse(dateString + " " + timeString);
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return null;
        }
    }

}
